<?php
namespace app\stock\controller;

use think\Controller;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->OrderModel = model('Order.Order');
        $this->VisitModel = model('Business.Visit');
        $this->ReceiveModel = model('Business.Receive');
        $this->SourceModel = model('Business.Source');

        $this->TimeList = [];

        $year = date("Y"); //2022

        // 时间封装
        for($i=1;$i<=12;$i++)
        {
            $time = strtotime($year."-".$i);

            //获取每个月的第一天
            $start = date("Y-m-01", $time);

            $end = date('Y-m-t', $time);

            $this->TimeList[] = [strtotime($start), strtotime($end)];
        }

    }

    // 查询总数
    public function total()
    {
        //总订单数
        $OrderTotal = $this->OrderModel->count();

        //算总和 销售额
        $AmountTotal = $this->OrderModel->sum('amount');

        // 客户总数
        $BusinessTotal = $this->BusinessModel->count();

        $result = [
            'OrderTotal' => $OrderTotal,
            'AmountTotal' => $AmountTotal,
            'BusinessTotal' => $BusinessTotal,
        ];

        $this->success('返回总数', null, $result);
    }

    public function business()
    {
        $status0 = [];
        $status1 = [];

        foreach($this->TimeList as $item)
        {
            //未认证
            $where = [
                'status' => 0,
                'createtime' => ['between', $item]
            ];

            $status0[] = $this->BusinessModel->where($where)->count();

            //已认证
            $where = [
                'status' => 1,
                'createtime' => ['between', $item]
            ];

            $status1[] = $this->BusinessModel->where($where)->count();
        }

        $this->success('返回客户统计', null, ['status0' => $status0, 'status1' => $status1]);
    }

    public function visit()
    {
        $VisitCount = [];

        foreach($this->TimeList as $item)
        {
            $where = [
                'createtime' => ['between', $item]
            ];

            $VisitCount[] = $this->VisitModel->where($where)->count();
        }

        $this->success('返回回访统计', null, $VisitCount);
    }

    public function receive()
    {
        //初始化数据
        $apply = $allot = $recovery = [];

        foreach($this->TimeList as $item)
        {
            $where = [
                'status' => 'apply', //申请
                'applytime' => ['between', $item]
            ];

            $apply[] = $this->ReceiveModel->where($where)->count();

            $where = [
                'status' => 'allot', //分配
                'applytime' => ['between', $item]
            ];

            $allot[] = $this->ReceiveModel->where($where)->count();

            $where = [
                'status' => 'recovery', //回收
                'applytime' => ['between', $item]
            ];

            $recovery[] = $this->ReceiveModel->where($where)->count();
        }

        $result = [
            'apply'=>$apply,
            'allot'=>$allot,
            'recovery'=>$recovery,
        ];

        $this->success('领取统计', null, $result);
    }

    public function order()
    {
        $status1 = $this->OrderModel->where(['status' => 1])->count();
        $status2 = $this->OrderModel->where(['status' => 2])->count();
        $status3 = $this->OrderModel->where(['status' => 3])->count();
        $status4 = $this->OrderModel->where(['status' => 4])->count();

        $result = [
            ['name'=> '已支付', 'value' => $status1],
            ['name'=> '已发货', 'value' => $status2],
            ['name'=> '已收货', 'value' => $status3],
            ['name'=> '已完成', 'value' => $status4],
        ];

        $this->success('订单统计', null, $result);
    }

    public function source()
    {
        $data = [];
        
        //先查询所有的来源
        $sourcelist = $this->SourceModel->select();

        foreach($sourcelist as $item)
        {
            $count = $this->BusinessModel->where(['sourceid' => $item['id']])->count();

            $data[] = [
                'name' => $item['name'],
                'value' => $count
            ];
        }

        $this->success('来源统计', null, $data);
    }
}
